<?php

namespace backend\controllers;

use common\models\Category;
use Yii;
use common\models\News;
use backend\models\NewsSearch;
use backend\components\CRUDController;
use yii\web\NotFoundHttpException;

/**
 * NewsController implements the CRUD actions for News model.
 */
class NewsController extends CRUDController
{
  public function actions()
  {
    return [
      'Kupload' => [
        'class' => '\jjbx\kindeditor\KindEditorAction',
        'attr_path' => 'news',
      ]
    ];
  }
    protected  function findModel($id = null,$search = false)
    {
        if($search){
            $model = new NewsSearch();
        }else if(empty($id)){
            $model = new News();
        }else{
            if (($model = News::findOne($id)) == null) {
                throw new NotFoundHttpException('The requested page does not exist.');
            }
        }
        return $model;
    }

  public function actionAjax($q=''){
    $arr = Category::findActive()->select(['id', 'title as text'])->filterWhere(['like', 'title', $q])
      ->asArray()
      ->limit(20)
      ->all();
    Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    return ['results'=>$arr];
  }
}
